package com.yixing.tech.common.tcp.channel;

import com.yixing.tech.common.tcp.handler.MessageHandler;
import com.yixing.tech.common.tcp.serialize.HexByteDecoder;
import com.yixing.tech.common.tcp.serialize.HexMsgEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;


import java.util.concurrent.TimeUnit;

public class ChannelInit extends ChannelInitializer<SocketChannel> {

    private final MessageHandler messageHandler;

    public ChannelInit(MessageHandler messageHandler) {
        this.messageHandler = messageHandler;
    }

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        socketChannel.pipeline()
                .addLast("idle", new IdleStateHandler(0, 0, 120, TimeUnit.SECONDS))
                .addLast(new HexByteDecoder())
                .addLast(new HexMsgEncoder())
                .addLast("messageHandler", messageHandler);
    }
}
